﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace MyGames
{
    public enum AttackType { Claw, Slash, Arrow, Magic }
    public enum Element { Dark, Light, Fire, Ice, Lighting }

    class EnemyProjectile : GameObject
    {
        static string[] attackTypePath = { "Claw", "Slash", "Arrow", "Magic" };
        static string[] elementPath = { "Dark", "Light", "Fire", "Ice", "Lightning" };
        static ContentManager content = GameLoader.Content;

        #region Declaration
        AttackType type;
        Element element;
        int value;
        int range;
        Vector2 startPos;
        #endregion

        #region Properties
        public AttackType AttackType
        {
            get { return type; }
            set { type = value; }
        }

        public Element Element
        {
            get { return element; }
            set { element = value; }
        }

        public int Value
        {
            get { return this.value; }
            set { this.value = value; }
        }

        public int Range
        {
            get { return range; }
            set { range = value; }
        }

        public Vector2 StartPosition
        {
            get { return startPos; }
            set { startPos = value; }
        }

        public Vector2 Velocity
        {
            get { return velocity; }
            set { velocity = value; }
        }

        public bool IsFlying
        { get { return enabled; } }
        #endregion

        #region Constructor
        public EnemyProjectile(AttackType type, Element element, int act)
        {
            this.type = type;
            this.element = element;

            string path = Const.TexturePathProject;
            if (type == AttackType.Slash || type == AttackType.Claw)
            {
                path += "CS";
                animations.Add("project", new AnimationStrip(content.Load<Texture2D>(path), 1, 1, "project"));
                return;
            }

            path += attackTypePath[(int)type] + "\\";
            if (act == 0 && type == MyGames.AttackType.Arrow)
            {
                path += "Arrow";
                animations.Add("project", new AnimationStrip(content.Load<Texture2D>(path), 1, 1, "project"));
            }
            else
            {
                path += elementPath[(int)element] + act.ToString();
                animations.Add("project", new AnimationStrip(content.Load<Texture2D>(path), 1, 5, "project"));
            }
        }
        #endregion

        #region Public methods
        public void StartProject()
        {
            worldLocation = startPos;
            velocity *= 0.6f;
            if (velocity.X > 0) flipped = true;

            PlayAnimation("project", false, type == MyGames.AttackType.Magic || type == MyGames.AttackType.Arrow, true);
            enabled = true;
        }

        public void StopProject()
        {
            enabled = false;
        }
        #endregion

        #region Update and Draw
        public override void Update(GameTime gameTime)
        {
            if ((worldLocation - startPos).Length() > range || worldLocation.X <= 5 || worldLocation.X >= Camera.WorldRectangle.Width - 5 || animations[currentAnimation].FinishedPlaying)
                enabled = false;

            base.Update(gameTime);
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            if (!enabled)
                return;

            SpriteEffects effect = SpriteEffects.None;
            if (flipped)
            {
                effect = SpriteEffects.FlipHorizontally;
            }

            Vector2 og = new Vector2(animations[currentAnimation].FrameWidth / 2, animations[currentAnimation].FrameHeight / 2);
            spriteBatch.Draw(
                animations[currentAnimation].Texture,
                Camera.WorldToScreen(WorldLocation),
                animations[currentAnimation].FrameRectangle,
                affectColor, 0f, og, scale, effect, drawDepth);
        }

        protected override Point offset()
        {
            return new Point(animations[currentAnimation].FrameWidth / 2, animations[currentAnimation].FrameHeight / 2);
        }
        #endregion
    }
}
